package fi.foyt.jabberd2json.demo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import fi.foyt.jabberd2json.AbstractObjectSetHandler;
import fi.foyt.jabberd2json.Filter;
import fi.foyt.jabberd2json.ObjectSet;
import fi.foyt.jabberd2json.ObjectSetType;
import fi.foyt.jabberd2json.ObjectSetUpdateMode;
import fi.foyt.jabberd2json.ObjectType;

public class StatusObjectSetHandler extends AbstractObjectSetHandler {

  /**
   * getType method determines the object set type this handler handles
   */
  @Override
  public ObjectSetType getType() {
    return ObjectSetType.STATUS;
  }
  
  /**
   * readObjectSets method is used to gather data for the get calls
   */
  @Override
  public List<ObjectSet> readObjectSets(String owner, Filter filter) {
    
    // First we create a list object for the result 
    List<ObjectSet> result = new ArrayList<ObjectSet>();
    
    // After that we gather all the necessary data. 
    
    Date lastLogin = (Date) getValue(owner, "lastLogin", filter);
    Date lastLogout = (Date) getValue(owner, "lastLogin", filter);
    String status = (String) getValue(owner, "status", filter);
    String xml = (String) getValue(owner, "xml", filter); 
    String show = (String) getValue(owner, "show", filter); 
    
    // After all the required data is collected we create result object set 
    ObjectSet objectSet = new ObjectSet();    
    objectSet.addField(createField("last-login", lastLogin ));
    objectSet.addField(createField("last-logout", lastLogout));
    objectSet.addField(createField(ObjectType.STRING, "status", status));
    objectSet.addField(createField(ObjectType.NAD, "XML", xml));
    objectSet.addField(createField(ObjectType.STRING, "show", show));
    
    // and add it to the list
    result.add(objectSet);
    
    // and finally we just return the list 
    return result;
  }
  
  /**
   * updateObjectSet method is used to store data from set calls
   */
  @Override
  public void updateObjectSet(String owner, Filter filter, ObjectSetUpdateMode updateMode, Map<String, String> values) {
    // updateMode tells is what type of call this is. PUT (insert), REPLACE (update) or DELETE call.
    
    switch (updateMode) {
      case PUT:
        // if call is put we should insert data as new values
        for (String key : values.keySet()) {
          insertValue(owner, key, getDateValue(values, values.get(key)), filter);
        }
      break;
      case REPLACE:
        // if call is replace we should update existing data
        for (String key : values.keySet()) {
          updateValue(owner, key, getDateValue(values, values.get(key)), filter);
        }
      break;
      case DELETE:
        // if call is delete we should delete existing object set
        deleteObjectSet(owner, filter);
      break;
    }
  }
 
  private Object getValue(String owner, String name, Filter filter) {
    // This method is here just for example. You should implement your own method 
    return null;
  }
  
  private void insertValue(String owner, String name, Object value, Filter filter) {
    // This method is here just for example. You should implement your own method
  }
  
  private void updateValue(String owner, String name, Object value, Filter filter) {
    // This method is here just for example. You should implement your own method
  }

  private void deleteObjectSet(String owner, Filter filter) {
    // This method is here just for example. You should implement your own method
  }
  
}
